package com.example.springboot2023nocv.controller;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot2023nocv.entity.HealthClock;
import com.example.springboot2023nocv.entity.Vaccine;
import com.example.springboot2023nocv.vo.HealthClockVo;
import com.example.springboot2023nocv.service.HealthClockService;
import com.example.springboot2023nocv.vo.DataView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
//@RestController
//@RequestMapping("/admin")
public class HealthClockController {

    @Autowired
    private HealthClockService healthClockService;

    //跳转页面
    @RequestMapping("/toHealthClock")
    public String toHealthClock(){
        return "admin/healthclock";
    }

    //查询所有打卡记录
//    @GetMapping("/admin/listHealthClock")
    @RequestMapping("/listHealthClock")
    @ResponseBody
    public DataView listHealthClock(HealthClockVo healthClockVo ){
        //查询所有带有模糊查询条件 带有分页
        IPage<HealthClock> page = new Page<>(healthClockVo.getPage(),healthClockVo.getLimit());
        QueryWrapper<HealthClock> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(ObjectUtils.isNotEmpty(healthClockVo.getUsername()),
                "username",healthClockVo.getUsername());
        queryWrapper.eq(ObjectUtils.isNotEmpty(healthClockVo.getPhone()),
                "phone",healthClockVo.getPhone());
        healthClockService.page(page,queryWrapper);
        return new DataView(page.getTotal(),page.getRecords());
    }


    //添加或者修改健康打卡记录数据
//    @PostMapping("/admin/addHealthClock")
    @RequestMapping("/addHealthClock")
    @ResponseBody
    public DataView addHealthClock(HealthClock healthClock) {
        boolean b = healthClockService.saveOrUpdate(healthClock);
        DataView dataView = new DataView();
        if (b){
            dataView.setCode(200);
            dataView.setMsg("添加成功！");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("添加失败！");
        return dataView;
    }

    @RequestMapping("/updateHealthClock")
    @ResponseBody
    public DataView updateHealthClock(HealthClock healthClock){
        healthClockService.updateById(healthClock);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("修改成功！");
        return dataView;
    }

    //删除数据
//    @DeleteMapping("/admin/deleteHealthClockById")
    @RequestMapping("/deleteHealthClockById")
    @ResponseBody
    public DataView deleteHealthClockById(Integer id){
        healthClockService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除数据成功！");
        return dataView;
    }



}
